package com.website.config;

import com.website.config.properties.WebsiteProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.servlet.MultipartConfigElement;
import java.io.File;

/**
 * 时间：2021/02/19
 * 创建人：yanqb
 */
@Configuration
public class MultipartConfig {

    @Autowired
    WebsiteProperties websiteProperties;

    @Bean
    public MultipartConfigElement multipartConfigElement() {
        MultipartConfigFactory factory = new MultipartConfigFactory();

        //判断是否有文件夹，没有就创建
        File file = new File(websiteProperties.getUploadTmp());
        if (!file.exists()) {
            file.getParentFile().mkdirs();
        }
        //临时文件路径，如：d:\temp\
        factory.setLocation(websiteProperties.getUploadTmp());
        return factory.createMultipartConfig();
    }
}
